//
//  ZYCCreateFolderViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/23.
//

#import "ZYCCreateFolderViewController.h"
#import "ZYCCreateFolderView.h"
@interface ZYCCreateFolderViewController ()

@property (nonatomic, strong)ZYCCreateFolderView *createFolderView;
@property (nonatomic, strong) UIButton *saveButton;
@end

@implementation ZYCCreateFolderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"新建文件夹";
    [self.view addSubview:self.createFolderView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveButton];
}


- (ZYCCreateFolderView *)createFolderView {
    if (!_createFolderView) {
        _createFolderView = [[ZYCCreateFolderView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
        _createFolderView.placeHolder = @"请输入文件夹名称";
    }
    return _createFolderView;
}

- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [[UIButton alloc] init];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:HEX_RGB(kBgWhiteColor) forState:UIControlStateNormal];
        _saveButton.titleLabel.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        [_saveButton addTarget:self action:@selector(clickSaveAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

- (void)clickSaveAction:(UIButton *)sender{
    if (self.type == ZYCFolderTypePublic) {
        [self addPublicFolder];
    }else{
        [self addPersonFolder];
    }


}

// 新增公共文件夹
- (void)addPublicFolder{
    if (self.createFolderView.text.length == 0) {
        [MrjToast makeToast:@"请输入文件夹名称" makeView:self.view];
        return;
    }
    NSDictionary *params = @{
        @"parentFolderId":@(self.parentFolderId),
        @"name":self.createFolderView.text
    };
    [MrjSVProgressHUD showWithStatus:@"保存中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_AddPublicFolder requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

//新增个人文件夹
- (void)addPersonFolder{
    if (self.createFolderView.text.length == 0) {
        [MrjToast makeToast:@"文件名不能为空" makeView:self.view];
        return;
    }
    NSDictionary *params = @{
        @"parentFolderId":@(self.parentFolderId),
        @"name":self.createFolderView.text
    };
    [MrjSVProgressHUD showWithStatus:@"保存中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_AddPersonFolder requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
    
}
/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
